#This code presents the results in Tables 9, 10, 11, 12, 13, and 14 in Appendix D
if(!dir.exists("tabs")){dir.create("tabs")}
if(!dir.exists("tabs/appendix")){dir.create("tabs/appendix")}

library(lubridate)
library(foreign)
library(ggthemes)
library(stats)
library(lmtest)
library(sandwich)
library(quanteda)
library(stm)
library(stats)
library(ggpubr)
library(tidyverse)
library(tidytext)
library(estimatr)
library(texreg)

gc()

load("k40_no_covars.RData")

beta = tidy(topic_model)
gamma = tidy(topic_model, matrix = "gamma")

meta = meta %>% mutate(document = article) %>% select(- article)

gamma = left_join(gamma, meta, by = "document")
gc()

gamma2 = gamma %>% 
  group_by(date, topic) %>% 
  summarise(avg = mean(gamma))

terms_final = tibble(topic = character(),
                     terms = character())

terms_final = terms_final %>% 
  add_row(topic = "communication", terms = c("added, said, communicate, details, electronic")) %>% 
  add_row(topic = "assad", terms = c("president, Assad, mister, Bashar, people, public, nation")) %>% 
  add_row(topic = "ISIS", terms = c("terrorism, organization, ISIS, group, armed")) %>% 
  add_row(topic = "temperature", terms = c("region, degree, south, temperature, increase")) %>% 
  add_row(topic = "elections", terms = c("election, constitution, people, president, candidate, democrat")) %>% 
  add_row(topic = "weather", terms = c("governorate, agiculture, regions, season")) %>% 
  add_row(topic = "foreign fighters", terms = c("Newspaper, British, Tunisian, French, intelligence")) %>% 
  add_row(topic = "United States", terms = c("America, united, states, president, Washington, Obama, military")) %>% 
  add_row(topic = "announcements", terms = c("public, first, SANA, day, thawrah")) %>% 
  add_row(topic = "conspiracies and plots", terms = c("Syria, Arab, people, conspiracy, Zionist, intervention, resistance")) %>% 
  add_row(topic = "election winners", terms = c("president, mister, Asad, Bashar, Damascus, people, leader, general")) %>% 
  add_row(topic = "legislation", terms = c("article, law, declaration, council, number, legislation")) %>% 
  add_row(topic = "attacks", terms = c("armed, group, terrorist, car, fire, security, citizen")) %>% 
  add_row(topic = "speeches", terms = c("said, politics, states, should, change, discuss, people")) %>% 
  add_row(topic = "victims", terms = c("aid, humanitarian, camp, red, refugee, provide")) %>% 
  add_row(topic = "Gulf", terms = c("Saud, Egypt, Qatar, Gulf, opposition, brother")) %>% 
  add_row(topic = "attacks and accidents", terms = c("explosion, crime, children, civilian, assault")) %>% 
  add_row(topic = "media", terms = c("media, ask, truth, channel, report, rights")) %>% 
  add_row(topic = "Europe", terms = c("Europe, union, France, Germany, nuclear")) %>% 
  add_row(topic = "Yemen", terms = c("Yemen, enemy, Saud, military, plane, bombing")) %>% 
  add_row(topic = "culture", terms = c("culture, Syria, world, exhibition, Arab, ceremony, history")) %>% 
  add_row(topic = "capitulation", terms = c("Aleppo, city, people, army, security, reconicilation, armed")) %>% 
  add_row(topic = "economy", terms = c("economy, industry, investment, trade, company, projects")) %>% 
  add_row(topic = "diplomacy", terms = c("relations, isit, state, delegation, meeting, foreign, people")) %>% 
  add_row(topic = "religion", terms = c("religion, Islam, Patriarch, Muslim, Christian, Sheikh")) %>% 
  add_row(topic = "transportation", terms = c("Jordan, sea, flying, borders, transport, airport, ship")) %>% 
  add_row(topic = "natural resources", terms = c("water, oil, company, electricity, gas, project, energy")) %>% 
  add_row(topic = "national unity", terms = c("nation, army, martyrs, sons, people, security")) %>% 
  add_row(topic = "Lebanon", terms = c("Lebanon, resistance, Israel, army, nation, government")) %>% 
  add_row(topic = "finance", terms = c("public, money, Syrian, Lira, company, bank, economy")) %>% 
  add_row(topic = "bureaucracy", terms = c("council, public, project, minister/ministry, committee")) %>% 
  add_row(topic = "education", terms = c("university, education, students, teach")) %>% 
  add_row(topic = "terrorism", terms = c("terrorism, terror, army, armed, group, destroy")) %>% 
  add_row(topic = "Israel and Palestine", terms = c("Israel, Palestine, occupation, Jerusalem, land")) %>% 
  add_row(topic = "Ba'th party", terms = c("nation, Arab, party, work, leadershpi, people, union")) %>% 
  add_row(topic = "Iraq", terms = c("Iraq, Baghdad, America, kill, injure, security")) %>% 
  add_row(topic = "Turkey", terms = c("Turkey, Erdogan, government, party, people, justice")) %>% 
  add_row(topic = "Russia", terms = c("Syria, Russia(n), states, foreign, crisis, politics")) %>% 
  add_row(topic = "Iran", terms = c("Iran, region, Islam, Tehran, politics")) %>% 
  add_row(topic = "regional politics", terms = c("Arab, Israel, peace, decision, united, nations, Palestine"))

topics = tibble(topic = 1:40, name = terms_final$topic)

gamma2 = gamma2 %>% ungroup %>% left_join(topics)

gamma2 = gamma2 %>% 
  ungroup() %>% 
  group_by(date, topic) %>% 
  arrange(date, topic) %>% 
  ungroup()

gamma2 = gamma2 %>% 
  select(- topic) %>% 
  rename(topic = name) %>% 
  spread(topic, avg)

gamma2 = gamma2 %>% mutate(treated = ifelse(date >= ymd("2011-03-15"), 1, 0))

# No Year Dummies ---------------------------------------------------------
mod1 = lm_robust(`Israel and Palestine` ~ treated, data = gamma2)
mod2 = lm_robust(assad ~ treated, data = gamma2)
mod3 = lm_robust(diplomacy ~ treated, data = gamma2)
mod4 = lm_robust(bureaucracy ~ treated, data = gamma2)

texreg(list(mod1, mod2, mod3, mod4), include.ci = F, 
       custom.model.names = c("Israel and Palestine", "Assad", "Diplomacy", "Bureaucracy"), omit.coef = "year",
       reorder.coef = c(2, 1),
       custom.coef.names = c("(Intercept)", "Revolution (March 15, 2011)"),
       caption = "Decline of Historically Common Topics", float.pos = "H", file = "tabs/appendix/historic.tex")


mod1 = lm_robust(`conspiracies and plots` ~ treated, data = gamma2)
mod2 = lm_robust(`national unity` ~ treated, data = gamma2)
mod3 = lm_robust(attacks ~ treated, data = gamma2)
mod4 = lm_robust(terrorism ~ treated, data = gamma2)

texreg(list(mod1, mod2, mod3, mod4), include.ci = F, 
       custom.model.names = c("Conspiracies and Plots", "National Unity", "Attacks", "Terrorism"), omit.coef = "year",
       reorder.coef = c(2, 1),
       custom.coef.names = c("(Intercept)", "Revolution (March 15, 2011)"),
       caption = "Rise of New Topics", float.pos = "H", file = "tabs/appendix/new.tex")

mod1 = lm_robust(Iran ~ treated, data = gamma2)
mod2 = lm_robust(Russia ~ treated, data = gamma2)
mod3 = lm_robust(Gulf ~ treated, data = gamma2)
mod4 = lm_robust(Turkey ~ treated, data = gamma2)

texreg(list(mod1, mod2, mod3, mod4), include.ci = F, 
       custom.model.names = c("Iran", "Russia", "Gulf", "Turkey"), omit.coef = "year", reorder.coef = c(2, 1),
       custom.coef.names = c("(Intercept)", "Revolution (March 15, 2011)"),
       caption = "Changing Focus on International Actors", float.pos = "H", file = "tabs/appendix/intl.tex")

# Year Dummies ------------------------------------------------------------
gamma2$year = as.factor(floor_date(gamma2$date, "year"))

mod1 = lm_robust(`Israel and Palestine` ~ treated + year, data = gamma2)
mod2 = lm_robust(assad ~ treated + year, data = gamma2)
mod3 = lm_robust(diplomacy ~ treated + year, data = gamma2)
mod4 = lm_robust(bureaucracy ~ treated + year, data = gamma2)

texreg(list(mod1, mod2, mod3, mod4), include.ci = F, 
       custom.model.names = c("Israel and Palestine", "Assad", "Diplomacy", "Bureaucracy"), omit.coef = "year",
       reorder.coef = c(2, 1),
       custom.coef.names = c("(Intercept)", "Revolution (March 15, 2011)"),
       caption = "Decline of Historically Common Topics", float.pos = "H", file = "tabs/appendix/historic1.tex")


mod1 = lm_robust(`conspiracies and plots` ~ treated + year, data = gamma2)
mod2 = lm_robust(`national unity` ~ treated + year, data = gamma2)
mod3 = lm_robust(attacks ~ treated + year, data = gamma2)
mod4 = lm_robust(terrorism ~ treated + year, data = gamma2)

texreg(list(mod1, mod2, mod3, mod4), include.ci = F, 
       custom.model.names = c("Conspiracies and Plots", "National Unity", "Attacks", "Terrorism"), omit.coef = "year",
       reorder.coef = c(2, 1),
       custom.coef.names = c("(Intercept)", "Revolution (March 15, 2011)"),
       caption = "Rise of New Topics", float.pos = "H", file = "tabs/appendix/new1.tex")


mod1 = lm_robust(Iran ~ treated + year, data = gamma2)
mod2 = lm_robust(Russia ~ treated + year, data = gamma2)
mod3 = lm_robust(Gulf ~ treated + year, data = gamma2)
mod4 = lm_robust(Turkey ~ treated + year, data = gamma2)

texreg(list(mod1, mod2, mod3, mod4), include.ci = F, 
       custom.model.names = c("Iran", "Russia", "Gulf", "Turkey"), omit.coef = "year", reorder.coef = c(2, 1),
       custom.coef.names = c("(Intercept)", "Revolution (March 15, 2011)"),
       caption = "Changing Focus on International Actors", float.pos = "H", file = "tabs/appendix/intl1.tex")


